//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.compressor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

/**
 * FileCompressor <br>
 * 
 * @author Aji
 */
public class FileCompressor {

	private static final String CLASS_NAME = FileCompressor.class.getName();
	private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);
	
	
	/**
	 * Comprimir archivos y guardar el archivo resultante en el FileSystem
	 * @param inFileNames Archivos a comprimir
	 * @param outFileName Archivo zip de salida
	 */
	public static void CompressFiles(String inFileNames, String outFileName) {
		String METHOD_NAME = "CompressFiles";
		
		log.entering(CLASS_NAME, METHOD_NAME);

		byte[] buf = new byte[1024];
		String[] files = inFileNames.split(",");


		try {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName));

			for (int i = 0; i<files.length; i++) {
				// Compress the files
	           	FileInputStream in = new FileInputStream(files[i]);
	    
	           	// Add ZIP entry to output stream.
	           	out.putNextEntry(new ZipEntry(files[i].substring(files[i].lastIndexOf(File.separator))));
	    
	           	// Transfer bytes from the file to the ZIP file
	           	int len;
	           	while ((len = in.read(buf)) > 0) {
	               	out.write(buf, 0, len);
	           	}
				
				// Complete the entry
				out.closeEntry();
				in.close();
			}

			out.close();			
		}
		catch (IOException excep) {
			log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "IOException: ", excep);
		}

		log.exiting(CLASS_NAME, METHOD_NAME);		
		
	}

	/**
	 * Comprimir archivos y devolver el archivo resultante como un array de bytes
	 * @param inFileNames Archivos a comprimir
	 * @return byte[] Archivo comprimido
	 */
	public static byte[] CompressFiles(String inFileNames) {
		String METHOD_NAME = "getCompressedFiles";
		byte[] result;

        log.entering(CLASS_NAME, METHOD_NAME);

		byte[] buf = new byte[1024];
		String[] files = inFileNames.split(",");
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		

		try {
			ZipOutputStream out = new ZipOutputStream(output);

			for (int i = 0; i<files.length; i++) {
				// Compress the files
	           	FileInputStream in = new FileInputStream(files[i]);
	    
	           	// Add ZIP entry to output stream.
	           	out.putNextEntry(new ZipEntry(files[i].substring(files[i].lastIndexOf(File.separator))));
	    
	           	// Transfer bytes from the file to the ZIP file
	           	int len;
	           	while ((len = in.read(buf)) > 0) {
	               	out.write(buf, 0, len);
	           	}
				
				// Complete the entry
				out.closeEntry();
				in.close();
			}

			out.close();			
			result = output.toByteArray();
			
		}
		catch (IOException excep) {
			log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "IOException: ", excep);
			return null;
		}


		log.exiting(CLASS_NAME, METHOD_NAME);
		return result;
	}	

}
